﻿@using System.Globalization
@model  Model.ModelConfig
@{
    ViewBag.Title = "复制模型";
    Layout = "~/Views/Shared/_Main.cshtml";
}
@section Head{}
<script type="text/javascript">
    var UrlArray = $.getUrlArray();
    var modelTypeList = new Array();
    var opts = {
        rows: 15,
        height: 450,
        para: { dbName: encodeURI(UrlArray["dbName"]) },
        idField: "GUID",
        columns: [
                    { field: 'DefaultView', title: '名称', width: 40 },
                    { field: 'TableDescription', title: '描述', width: 40 },
                    { field: 'CreateTime', title: '创建时间', width: 10, align: 'center' },
                    { field: 'GUID', title: '操作', width: 10, align: 'center', formatter: function (value, rowData, rowIndex) {
                        var add = true; ;
                        for (var j = 0; j < modelTypeList.length; j++) {
                            if (modelTypeList[j].GUID == rowData.GUID) {
                                add = false;
                            }
                        }
                        if (add) {
                            modelTypeList.push(rowData);
                        }
                        return "<a href='#' onclick='remove(\"" + rowIndex + "\")'>删除</a>";
                    }
                    }
                    ]
    };
    function AddItem() {
        showModalMany('Contact', '/ModelConfig/ModelList?type=1&dbName=' + encodeURI(UrlArray["dbName"]), '800', '480', returnFun);
    }
    function returnFun(v) {
        for (var i = 0; i < v.length; i++) {
            var has = false;
            for (var j = 0; j < modelTypeList.length; j++) {
                if (v[i].GUID == modelTypeList[j].GUID) {
                    has = true;
                    break;
                }
            }
            if (has == false) {
                modelTypeList.push(v[i]);
            }
        }
        var data = { "total": 1, "rows": modelTypeList };
        opts["data"] = data;
        $("#DataGrid").dg(opts);
    }
    function back() {
        window.location = "/ModelConfig/Index?dbName=" + encodeURI(UrlArray["dbName"]);
    }
    function save() {
        $("#msg").html("");
        if ($.trim($("#txtTableName").val()) == "") {
            alert("模型名称不能为空。");
            return;
        }
        var typeIDs = new Array()
        for (var j = 0; j < modelTypeList.length; j++) {
            typeIDs.push(modelTypeList[j].GUID);
        }
        var para = { 
            Name: $("#txtTableName").val(),
            Description: $("#txtDescription").val(),
            AddType: "Copy",
            IsRecord: 0,
            ModelTypeID: typeIDs.join(","),
            ModelRefID: UrlArray["modelID"],
            dbName: encodeURI(UrlArray["dbName"])
        };
        $("#loading").show();
        $.post("/ModelConfig/SaveModelData", para, function (result) {
            if (result[0] == "ok") {
                alert("操作成功！"); 
            }
            else if (result[0] == "2") {
                alert("复制模型配置成功！\r\n执行模型脚本失败！");
            }
            else
                $("#msg").html(result);
            $("#loading").hide();
        });
    }
</script>
<div style="margin: 5px;">
    <table class="listTitle">
        <tr>
            <td width="15">
                <img src="/images/edit.png" />
            </td>
            <td width="300">
                复制模型
            </td>
            <td align="right">
                <input type="button" class="button" value="保存" onclick="save()" />
                <input type="button" class="button" value="返回" onclick="back()" />
            </td>
        </tr>
    </table>
    <table class="searchArea" width="100%">
        <tr>
            <td>
                原模型名称：@Model.TableName<br />
                新模型名称：<input type="text" id="txtTableName" style="width: 350px;" /><span class="red">*</span><br /> 
                新模型描述：<textarea id="txtDescription" style="width: 350px; height: 50px;"></textarea> 
            </td>
        </tr>
    </table>
    <table class="listTitle">
        <tr>
            <td width="15">
                <img src="/images/edit.png" />
            </td>
            <td width="300">
                模型标签
            </td>
            <td align="right">
                <input type="button" value="选择模型标签" onclick="AddItem();" class="button" />
            </td>
        </tr>
    </table>
    <table id="DataGrid" class="pagTable" cellpadding="0" cellspacing="0">
    </table>    
    <div id="msg">
    </div>
    <div id="loading" class="loading">
        <img src="/Images/tree_loading.gif" />
    </div>
</div>
